<template>
    <div class="app-container">
        <!-- 表格-条件筛区域  -->
        <div class="filter-container">
            <el-input placeholder="请输入用户名" v-model="searchParam.SEARCH_LIKE_user_name" style="width: 200px;"
                      class="filter-item" @keyup.enter.native="handleFilter"/>
            <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter"> 搜索</el-button>
        </div>

        <!-- 表格-内容区域 -->
        <el-table v-loading="listLoading" :key="tableKey" :data="tableList" border fit highlight-current-row
                  style="width: 100%;">
            <el-table-column v-for="column in columns" :key="column.prop"
                             :prop="column.prop"
                             :label="column.label"
                             :width="column.width"
                             :align="column.align?column.align:'center'"
                             :min-width="column.minWidth"
                             :sortable="column.sortable"
                             :fixed="column.fixed"
                             :type="column.type" :formatter="column.formatter">
                <template slot-scope="scope">
                    <!-- 行内显示图片 -->
                    <img v-if="column.imgFlag==true"
                         :src=" scope.row[column.prop].indexOf('http://') >-1 ?  scope.row[column.prop] : ( CMBASE.baseFileContext + scope.row[column.prop] ) "
                         width="60" height="60" class="img"/>
                    <!-- 行内显示开关按钮 -->
                    <el-switch v-else-if="column.switchFlag==true" v-model="scope.row[column.prop]" :active-value="1"
                               :inactive-value="0" disabled/>
                    <!-- 行内显示tag标签 -->
                    <span v-else-if="column.tagFlag==true">
                        <span v-for="item in column.tagFormatter" :key="item.value">
                            <el-tag v-if="item.value==scope.row[column.prop]" :type="item.type">{{ item.text }}</el-tag>
                        </span>
                    </span>
                    <!-- 行内格式自定义 -->
                    <span v-else-if="column.formatter"> {{ column.formatter(scope.row) }}</span>
                    <!--默认显示文本-->
                    <span v-else>{{ scope.row[column.prop] }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="380" class-name="small-padding">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="handleExchange(scope.row.id,1)"> 金币使用记录</el-button>
                    <el-button type="danger" size="mini" @click="handleExchange(scope.row.id,2)"> 用户充值记录</el-button>
                    <el-button type="primary" size="mini" @click="handleExchange(scope.row.id,0)"> 收入支出记录</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 表格-页脚区域 -->
        <pagination v-show="tableTotal>0" :total="tableTotal" :page.sync="pageRequest.pageNum"
                    :limit.sync="pageRequest.pageSize" @pagination="getList"/>

        <!-- 收入支出记录-->
        <el-dialog :title="exchangeListName" :visible.sync="getExchangeListVisible" :close-on-click-modal="false"
                   width="65%" top="5vh">
            <el-table v-loading="listLoadingExchange" :data="tableListExchange" border fit highlight-current-row
                      style="width: 100%;">
                <el-table-column
                    v-for="column in columnExchange"
                    :key="column.prop"
                    :prop="column.prop"
                    :label="column.label"
                    :width="column.width"
                    :align="column.align?column.align:'center'"
                    :min-width="column.minWidth"
                    :sortable="column.sortable"
                    :fixed="column.fixed"
                    :type="column.type"
                    :formatter="column.formatter">
                    <template slot-scope="scope">
                        <img v-if="column.imgFlag==true" :src="scope.row[column.prop]" width="60" height="60"
                             class="img">
                        <!-- 行内显示开关按钮 -->
                        <el-switch v-else-if="column.switchFlag==true" v-model="scope.row[column.prop]"
                                   :active-value="1"
                                   :inactive-value="0"
                                   disabled/>
                        <!-- 行内显示tag标签 -->
                        <span v-else-if="column.tagFlag==true">
                            <span v-for="item in column.tagFormatter" :key="item.value">
                                <el-tag v-if="item.value==scope.row[column.prop]" :type="item.type">{{
                                        item.text
                                    }}</el-tag>
                            </span>
                        </span>
                        <!-- 行内格式自定义 -->
                        <span v-else-if="column.formatter"> {{ column.formatter(scope.row) }}</span>
                        <span v-else-if="column.prop === 'source'">
                            {{ scope.row['source'] | formatSource(sourceList) }}
                        </span>
                        <!--默认显示文本-->
                        <span v-else>{{ scope.row[column.prop] }}</span>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 表格-页脚区域 -->
            <pagination v-show="tableTotalExchange>0" :total="tableTotalExchange"
                        :page.sync="pageRequestExchange.pageNum"
                        :limit.sync="pageRequestExchange.pageSize" @pagination="showExchangeList"/>

            <div slot="footer" class="dialog-footer">
                <el-button size="mini" @click.native="exchangeCloseDialog()">关闭</el-button>
            </div>
        </el-dialog>

    </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 分页栏信息
import {getAccountRecordList, getAccountSourceList, getVUserAccountList} from '@/api/modules/appUser'

export default {
    name: 'userAccount',
    components: {
        Pagination,
    },
    directives: {},
    filters: {
        formatSource(value, list) {
            const item = list.find(item => item.id === value)
            return item ? item.name : ""
        }
    },
    data() {
        return {
            tableKey: 0,
            tableList: [],//表格数据源
            tableTotal: 0,//表格总条数数据
            listLoading: true,
            pageRequest: {pageNum: 1, pageSize: 10,},
            searchParam: {},
            dialogFormVisible: false,//默认不显示弹出框
            operation: true, // true:新增, false:编辑
            editLoading: false,//保存表单时loading框
            columns: [
                // {prop: 'id',  label: '',  sortable:false  },
                {prop: 'userName', label: '用户名', sortable: false},
                {prop: 'amount', label: '余额', sortable: false},
                // {prop: 'ct',  label: '创建时间',  sortable:false  },
                {prop: 'et', label: '更新时间', sortable: false},
            ],

            userId: '', // userId
            //收入支出记录
            exchangeListName: '收入支出记录',
            getExchangeListVisible: false, // 属性值列表页 隐藏
            operationExchange: true, // true:新增, false:编辑
            pageRequestExchange: {pageNum: 1, pageSize: 10},
            searchParamExchange: {}, // 属性key对应value 搜索条件
            dialogFormVisibleExchange: false, // 默认不显示弹出框
            listLoadingExchange: true,
            tableListExchange: [], // 表格数据源
            tableTotalExchange: 0,
            columnExchange: [
                // {prop: 'userId', label: '用户id', sortable: false},
                {prop: 'amount', label: '金额', sortable: false},
                {
                    prop: 'changeType', label: '变动类型', sortable: false, tagFlag: true,
                    tagFormatter: [
                        {text: '支出', value: 1, type: 'danger'},
                        {text: '收入', value: 2, type: 'success'}
                    ]
                },
                {prop: 'ct', label: '时间', sortable: false},
                // {prop: 'orderId', label: '订单id', sortable: false},
                {prop: 'remark', label: '备注', sortable: false},
                {prop: 'source', label: '变动来源', sortable: false},
            ],
            sourceList: [],
            exchangeType:'',
        }
    },
    methods: {
        //获取表格数据
        getList() {
            this.listLoading = true
            //【API请求】----获取表格列表数据
            getVUserAccountList(this.pageRequest).then(response => {
                response = response.result
                this.tableList = response.content
                this.tableTotal = response.total
                this.listLoading = false;
            }).catch((res) => {
                this.$message({message: res.msg, type: 'error'})
            })
        },
        getSourceList() {
            getAccountSourceList().then(response => {
                response = response.result
                this.sourceList = response.content
            }).catch((res) => {
                this.$message({message: res.msg, type: 'error'})
            })
        },
        //获取表格数据
        handleExchange(userId, type) {
            this.exchangeType = type
            this.userId = userId
            this.listLoadingExchange = true
            this.getExchangeListVisible = true
            //【API请求】----获取表格列表数据
            const params = {
                userId: userId,
                type: type,
                pageNum: this.pageRequestExchange.pageNum,
                pageSize: this.pageRequestExchange.pageSize
            }
            getAccountRecordList(params).then(response => {
                response = response.result
                this.tableListExchange = response.list
                this.tableTotalExchange = response.total
                this.listLoadingExchange = false;
            }).catch((res) => {
                this.$message({message: res.msg, type: 'error'})
            })
        },
        showExchangeList() {
            this.handleExchange(this.userId,this.exchangeType)
        },
        //表格数据筛选
        handleFilter() {
            this.pageRequest.searchParam = this.searchParam
            this.getList()
        },
        //初始化表单数据，重新渲染
        initDataForm() {
            this.dataForm = {
                id: '',
                userName: '',
                amount: '',
                ct: '',
                et: '',
            };
        },
        //关闭表单弹出框
        closeDialog() {
            this.$refs['dataForm'].resetFields();
            this.dialogFormVisible = false
        },
        exchangeCloseDialog() {
            this.getExchangeListVisible = false
        },

    },
    created() {
        this.getList();
        this.getSourceList();
    },
}
</script>
